package cn.rkylin.oms.category.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.category.vo.GoodsCategoryInfoVO;
import cn.rkylin.oms.common.annotation.SystemControllerLog;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.goods.controller.GoodsController;
import cn.rkylin.oms.goods.service.IGoodsCategoryService;

@Controller
@RequestMapping("/goodsCategory")
public class GoodsCategoryController extends ApolloController {
    private static final Log logger = LogFactory.getLog(GoodsController.class);
    // 常量定义
    private static final String RESULT_SUCCESS_FLG = "success";
    private static final String RESULT_ERROR_MESSAGE = "errormessage";
    private static final String RESULT_ROW_COUNT = "rowcount";
    /**
     * 系统商品服务层
     */
    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    /**
     * 数据处理
     * 
     * @param goodsCategoryInfoVO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/updateGoodsCategory")
    @SystemControllerLog(module = "系统商品", operation = "商品分类更改", keyCode = "key")
    public Map<String, Object> updateGoods(@RequestBody GoodsCategoryInfoVO goodsCategoryInfoVO) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return resultMap;
        }

        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return resultMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }
        mapParm.put("entId", entId);

        Map<String, Object> map = goodsCategoryService.modifyData(goodsCategoryInfoVO, mapParm);
        resultMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
        resultMap.put(RESULT_ROW_COUNT, map.get("count"));
        resultMap.put(RESULT_ERROR_MESSAGE, map.get("errorMessage"));
        resultMap.put("key", goodsCategoryInfoVO.getKey());
        return resultMap;
    }

    /**
     * 数据处理
     * 
     * @param key
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/delGoodsCategory")
    @SystemControllerLog(module = "系统商品", operation = "商品分类删除", keyCode = "key")
    public Map<String, Object> delGoodsCategory(String key) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();

        Map<String, Object> map = goodsCategoryService.deleteData(key);
        resultMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
        resultMap.put(RESULT_ROW_COUNT, map.get("count"));
        resultMap.put(RESULT_ERROR_MESSAGE, map.get("errorMessage"));
        resultMap.put("key", key);
        return resultMap;
    }

    /**
     * 下载
     *
     * @return map response body
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = { "/downLoadGoodsCategory" })
    public Map<String, Object> downLoadGoodsCategory() throws Exception {

        Map<String, Object> map = goodsCategoryService.downLoadGoodsCategory("51f17f6ddbae11e68c4d001e67c7ec3a","");

        return map;
    }

    /**
     * 数据处理
     * 
     * @param newParentkey
     * @param key
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/changeGoodsCategory")
    @SystemControllerLog(module = "系统商品", operation = "商品分类更改", keyCode = "key")
    public Map<String, Object> changeGoodsCategory(String newParentkey, String key) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();

        Map<String, Object> map = goodsCategoryService.changeParentKey(key, newParentkey);
        resultMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
        resultMap.put(RESULT_ROW_COUNT, map.get("count"));
        resultMap.put(RESULT_ERROR_MESSAGE, map.get("errorMessage"));
        resultMap.put("key", key);
        return resultMap;
    }

    /**
     * 判断是否有新增，修改
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/isadd")
    @SystemControllerLog(module = "系统商品", operation = "判断按钮", keyCode = "key")
    public Map<String, Object> isadd() throws Exception {

        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return resultMap;
        }

        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return resultMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }

        resultMap.put(RESULT_SUCCESS_FLG, "y");
        if ("51f17f6ddbae11e68c4d001e67c7ec3a".equals(entId)){
            resultMap.put("isadd","n");
        } else {
            resultMap.put("isadd","y");
        }

        return resultMap;
    }
}
